package top.mable.rabbitmq.listener;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.mable.rabbitmq.dto.MqMsg;

import java.io.IOException;
import java.util.Map;

/**
 * @author taffywen
 * @date 2022/7/24 21:52
 */

@Component
@Slf4j
public class DeadLetterMQ {

    @Autowired
    private RabbitTemplate rabbitTemplate;


    public void sendQueueA(Map map){

        MqMsg msg = new MqMsg();
        msg.setMethod("sendQueueA-----------------");
        msg.setId(UUID.fastUUID().toString());
        msg.setContent(JSONUtil.toJsonStr(map));
        msg.setRoutingKey("queue.a");
        msg.setExchange("rabbitMqExchange");

        rabbitTemplate.convertAndSend("rabbitMqExchange","queue.a",msg);

    }




    @RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(value = "rabbitMqExchange",type = ExchangeTypes.TOPIC),
            value = @Queue(value = "queueA",durable = Exchange.TRUE,autoDelete = Exchange.FALSE,
                    arguments = {@Argument(name = "x-dead-letter-exchange",value = "dead_direct_exchange"),
                            @Argument(name = "x-dead-letter-routing-key", value = "dead-key")}),
            key = "queue.a")})
    public void getMsg(Message message, Channel channel) throws IOException {
        MqMsg unserialize = ObjectUtil.unserialize(message.getBody());
        log.info("sendQueueA，内容为{}",unserialize.toString());
//        throw new RuntimeException();
        channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
    }


    /**
     * 死信监听
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitListener(bindings = {@QueueBinding(
            exchange = @Exchange(value = "dead_direct_exchange",type = ExchangeTypes.TOPIC),
            value = @Queue(value = "dead.direct.queue",durable = Exchange.TRUE,autoDelete = Exchange.FALSE),
            key = "dead-key")})
    public void consumeDelayMsg(Message message, Channel channel) throws IOException {
        log.info("进入--MQDelayedMsgListener死信队列  {}",message.toString());
        MqMsg mqMsg = ObjectUtil.unserialize(message.getBody());
        log.info("反序列化的-MGg{}-",mqMsg);

        String forwardExchange = mqMsg.getForwardExchange();
        String forwardRoutingKey = mqMsg.getForwardRoutingKey();
        if (StrUtil.isNotEmpty(forwardExchange) && StrUtil.isNotEmpty(forwardRoutingKey)){
            log.info("进入延时队列");
            mqMsg.setForwardExchange(null);
            mqMsg.setForwardRoutingKey(null);
            rabbitTemplate.convertAndSend(forwardExchange,forwardRoutingKey,mqMsg);
        }
        //丢入另一个队列中
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
